﻿using AuroraPlatform.ViewModel;
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;

namespace AuroraPlatform
{
    public class ZeroValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return string.Empty;

            double dValue;
            double.TryParse(value.ToString(), out dValue);
            if (dValue == 0)
                return string.Empty;
            return string.Format("{0}{1}", value, parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class AngleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return string.Empty;

            int dValue;
            if (int.TryParse(value.ToString(), out dValue))
                return dValue.ToString() + "°";  
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return string.Empty;

            int dValue;
            if (int.TryParse(value.ToString(), out dValue))
                return dValue.ToString() + "K";
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public abstract class ResourceConverter : IValueConverter
    {
        protected abstract IEnumerable<ResItem> Resource { get; }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int id;
            if (int.TryParse(value == null ? string.Empty : value.ToString(), out id))
            {
                ResItem item = (from r in Resource
                                where r.ItemID == id
                                select r).FirstOrDefault();
                if (item != null)
                    return item.ItemName;

            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ProdTypeConverter : ResourceConverter
    {
        private IEnumerable<ResItem> _resource;

        protected override IEnumerable<ResItem> Resource
        {
            get
            {
                if (_resource == null)
                {
                    ResourceProvider provider = new ResourceProvider();
                    _resource = provider.ProdTypes;
                }
                return _resource;
            }
        }
    }

    public class UseAreaConverter : ResourceConverter
    {
        private IEnumerable<ResItem> _resource;

        protected override IEnumerable<ResItem> Resource
        {
            get
            {
                if (_resource == null)
                {
                    ResourceProvider provider = new ResourceProvider();
                    _resource = provider.UseArea;
                }
                return _resource;
            }
        }
    }

    public class OrginConverter : ResourceConverter
    {
        private IEnumerable<ResItem> _resource;

        protected override IEnumerable<ResItem> Resource
        {
            get
            {
                if (_resource == null)
                {
                    ResourceProvider provider = new ResourceProvider();
                    _resource = provider.Orgin;
                }
                return _resource;
            }
        }
    }

    public class ImpactTypeConverter : ResourceConverter
    {
        private IEnumerable<ResItem> _resource;

        protected override IEnumerable<ResItem> Resource
        {
            get
            {
                if (_resource == null)
                {
                    ResourceProvider provider = new ResourceProvider();
                    _resource = provider.ImpactType;
                }
                return _resource;
            }
        }
    }

    public class LightingExplainConverter : ResourceConverter
    {
        private IEnumerable<ResItem> _resource;

        protected override IEnumerable<ResItem> Resource
        {
            get
            {
                if (_resource == null)
                {
                    ResourceProvider provider = new ResourceProvider();
                    _resource = provider.LightingExplain;
                }
                return _resource;
            }
        }
    }

    public class WorkVoltageConverter : ResourceConverter
    {
        private IEnumerable<ResItem> _resource;

        protected override IEnumerable<ResItem> Resource
        {
            get
            {
                if (_resource == null)
                {
                    ResourceProvider provider = new ResourceProvider();
                    _resource = provider.WorkVoltage;
                }
                return _resource;
            }
        }
    }

    public class ControlTypeConverter : ResourceConverter
    {
        private IEnumerable<ResItem> _resource;

        protected override IEnumerable<ResItem> Resource
        {
            get
            {
                if (_resource == null)
                {
                    ResourceProvider provider = new ResourceProvider();
                    _resource = provider.ControlType;
                }
                return _resource;
            }
        }
    }

    public class LightingTypeConverter : ResourceConverter
    {
        private IEnumerable<ResItem> _resource;

        protected override IEnumerable<ResItem> Resource
        {
            get
            {
                if (_resource == null)
                {
                    ResourceProvider provider = new ResourceProvider();
                    _resource = provider.LightingType;
                }
                return _resource;
            }
        }
    }

    public class SetupTypeConverter : ResourceConverter
    {
        private IEnumerable<ResItem> _resource;

        protected override IEnumerable<ResItem> Resource
        {
            get
            {
                if (_resource == null)
                {
                    ResourceProvider provider = new ResourceProvider();
                    _resource = provider.SetupType;
                }
                return _resource;
            }
        }
    }
}
